/*---------------------------------------------------------------------------*/
/*  RegExp class :                                                           */
/*  Regular expressions by gogoprog @ gmail . com                            */
/*---------------------------------------------------------------------------*/
#include "RegExp.h"
/*---------------------------------------------------------------------------*/
RegExp::RegExp(string regexp)
{
      strRegExp = regexp;
}
/*---------------------------------------------------------------------------*/
RegExp::~RegExp()
{
      deleteData();
}
/*---------------------------------------------------------------------------*/
bool RegExp::match( string source )
{
	deleteData();
	
	bool quit = false;
	bool passBlank = true;
	string temp;
	unsigned int i=0, j=0;
	

		while( !quit )
		{
			
			
			if( passBlank && ( source[i] == ' ' || strRegExp[j] == ' ') )
			{
				if( source[i] == ' ' ) i++;
				if( strRegExp[j] == ' ' ) j++;
				
				//cout << "passblank" << endl;
				if( j + 1 > strRegExp.length() ) return true;
				
			}
			else
			{
			
				//cout << "else " << i << " - " <<j <<endl;
				
				if( j + 1 > strRegExp.length() ) return true;
				if( i + 1 > source.length() ) return false;
				
				switch(  strRegExp[j] )
				{
					case '\0': 
						return true; 
					break;
					
					case '?':


						vData.push_back( reinterpret_cast<void*>(new char( source[i] ) ) );
						vType.push_back( '?' );
						//cout << "char parsed!" << endl;
						j++;
						i++;
					break;
				
					
					case '*':
					
						temp = "";
						//cout << "string" <<endl;
						//if( i >= source.length() || j+1 >= strRegExp.length() ) return false;

						while(source[i] && source[i] != strRegExp[j+1] )
						{
							//cout << i << " - " <<j  << endl;
							temp += source[i];
							//cout << temp << endl;
							if( !source[i+1] ) break;
							i++;
						}
					
						//cout << temp << "|" << endl;
					
						vData.push_back( reinterpret_cast<void*>(new string( temp ) ) );
						vType.push_back( '*' );
						j++;
						//i++;
					break;
													
					default:
						//cout << "default " << i << " - " << j << " " <<  source[i] <<  " " << strRegExp[j] << endl;
						
						//if( !strRegExp[j] ) return true; // end of reg exp reached
						
						if( strRegExp[j] == source[i] && source[i] && strRegExp[j])
						{
							i++;
							j++;
							
						}
						else
						{
							//cout << "oui" << endl;
							//quit = true;
							return false;
						}
					break;		
				}
			}
		
			
			
		}

	
	
	return false;
}
/*---------------------------------------------------------------------------*/void RegExp::deleteData()
{
	for(unsigned int i=0; i < vData.size(); i++)
	{
		if(vType[i] == '?') delete reinterpret_cast<char*>(vData[i]);
		if(vType[i] == '*') delete reinterpret_cast<string*>(vData[i]);
	}
	
	vData.clear();
	vType.clear();
}
/*---------------------------------------------------------------------------*/     
// get data pointer (void*)
void *RegExp::getData(char i)
{
	return vData[i];
}
/*---------------------------------------------------------------------------*/     
int RegExp::getInt(char i)
{
	switch( vType[i] )
	{
		case '?':
			return *((char*)vData[i]) - '0';
		break;
		
		case '*':
			string str = getString(i);
			return atoi( str.c_str() );
		break;
	
	}
	
	return -1;
}
/*---------------------------------------------------------------------------*/  
string RegExp::getString(char i)
{
	return *((string*)vData[i]);
}
/*---------------------------------------------------------------------------*/  
char RegExp::getChar(char i)
{
	return *((char*)vData[i]);
}	 
/*---------------------------------------------------------------------------*/  	




      

